<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_aihire/model')->方法名();
*/
namespace jy_aihire;
defined('BY_JYA') or exit('error');
class job extends \plugin{
	public function getjobs($parameter,$limit=5,$lat_lng=array(),$page=1,$limitjuli=0){
		$limitjuli=intval($limitjuli);
		if($lat_lng['lat'] || $lat_lng['lng']){
			$lat_lng=array('lat'=>$lat_lng['lat'],'lng'=>$lat_lng['lng']);
			$this->session('lat_lng',$lat_lng);
		}
		$lat_lng=$this->session('lat_lng');
		$total='';
		$parameter['orderby']=$parameter['orderby']?:'id desc';
		$isonlinetime=$parameter['params']['isonlinetime'];
		unset($parameter['params']['isonlinetime']);
		$list=pdo_getslice('jy_aihire_merch',$parameter['params'],$parameter['limit'],$total,$parameter['fields'],$parameter['key'],$parameter['orderby']);
		$start_goodsnum=$this->setting['start_goodsnum'];
		foreach($list as $k=>&$v){
			$v['pcd']=json_decode($v['pcd'],true);
			$v['lat_lng']=json_decode($v['lat_lng'],true);
			$v['lat_lng']['lng']=$v['lat_lng']['lng']?:'114.424632';
			$v['lat_lng']['lat']=$v['lat_lng']['lat']?:'23.126942';
			$v['juli']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$v['lat_lng']['lng'],$v['lat_lng']['lat']);
			$v['juli']=intval($v['juli']/1000)?:2;
			if($limitjuli && $v['juli']>$limitjuli){
				unset($list[$k]);
				continue;
			}
			if($isonlinetime){
				$v['onlinetime']=json_decode($v['onlinetime'],true);
				$v['onlinetime']['start']=$v['onlinetime']['start']?:'7:00';
				$v['onlinetime']['end']=$v['onlinetime']['end']?:'22:00';	
				$star=strtotime($days.' '.$v['onlinetime']['start']);
				$end=strtotime($days.' '.$v['onlinetime']['end']);
				if($end<$star){
					$end=$end+24*3600;
				}
				if(($star>TIMESTAMP || TIMESTAMP>$end)){
					$v['offline']=1;
				}
			}
			if($v['offline']==1 && ($this->setting['no_offline'] || $parameter['no_offline'])){
				unset($list[$k]);
				continue;
			}
			$v['paixu']=$v['juli'];
			if($v['isrecommand']){
				$v['paixu']=$v['juli']>30?$v['juli']-30:0;
			}
			if($parameter['orderbykey']=='star' && $v['star']>0){
				$v['paixu']=$v['juli']>30?$v['juli']-30:0;
				$paixu=5/$v['star'];
				$v['paixu']=$v['paixu']+$paixu;
			}
            if($parameter['orderbykey']=='salenum' || $parameter['orderbykey']=='istuijie'){
                $v['paixu']=0-$v['salenum'];
            }
            if($parameter['orderbykey']=='out_price'){
                $v['paixu']=0-$v['out_price'];
            }
            if($parameter['orderbykey']=='out_pay'){
                $v['paixu']=0-$v['out_pay'];
            }
			if($v['offline']){
				$v['paixu'] +=500;
			}
		}
		paixu($list,'paixu');
		if($limit){
			$list=array_slice($list,0,intval($limit));
		}else{
			$page=$page?:1;
			$start=($page-1)*10;
			$list=array_slice($list,$start,10);
		}
		return $list;
	}
	public function upgong($job_log,$status=0){
		$item=pdo_get('jy_aihire_gong',array('joblogid'=>$job_log['id'],'uniacid'=>$_SESSION['uniacid']));
		if($item['id'] && $status!=1){
			pdo_delete('jy_aihire_gong',array('joblogid'=>$job_log['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($status==1 && !$item['id']){
			$job=pdo_get('jy_aihire_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$job_log['jobid']),array('title','lat_lng','province','city','district','address','onlinetime','pid','cid','sex','type','paytype','price','pay','baotype'));
			$onlinetime=$job['onlinetime'];
			$pcd=array('province'=>$job['province'],'city'=>$job['city'],'district'=>$job['district']);
			$lat_lng=$job['lat_lng'];
			unset($job['onlinetime']);
			unset($job['lat_lng']);
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'memberid'=>$job_log['memberid'],
				'mid'=>$job_log['mid'],
				'merchid'=>$job_log['merchid'],
				'jobid'=>$job_log['jobid'],
				'type'=>$job['type'],
				'paytype'=>$job['paytype'],
				'joblogid'=>$job_log['id'],
				'fee'=>$job['price'],
				'jobinfo'=>ijson_encode($job),
				'onlinetime'=>$onlinetime,
				'pcd'=>ijson_encode($pcd),
				'lat_lng'=>$lat_lng,
				'address'=>$job['address'],
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_aihire_gong',$insert);
			pdo_update('jy_aihire_job_log',array('status'=>1),array('id'=>$item['joblogid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function uphetong($gong,$status=0){
		$item=pdo_get('jy_aihire_hetonglist',array('gongid'=>$gong['id'],'uniacid'=>$_SESSION['uniacid']));
		if($item['id'] && $status!=1){
			pdo_delete('jy_aihire_hetonglist',array('gongid'=>$gong['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		if($status==1 && !$item['id']){
			$member=pdo_get('jy_aihire_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gong['memberid']),array('realname','nickname','mobile','codesn'));
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'memberid'=>$gong['memberid'],
				'mid'=>$gong['mid'],
				'merchid'=>$gong['merchid'],
				'gongid'=>$gong['id'],
				'type'=>$gong['type'],
				'hetplid'=>$gong['hetplid'],
				'images'=>ijson_encode($gong['images']),
				'usename'=>$member['realname'],
				'mobile'=>$member['mobile'],
				'codesn'=>$member['codesn'],
				'status'=>2,
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_aihire_hetonglist',$insert);
		}
		return true;
	}
	public function startsign($gong,$post=array()){
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'memberid'=>$gong['memberid'],
				'mid'=>$gong['mid'],
				'merchid'=>$gong['merchid'],
				'gongid'=>$gong['id'],
				'jobid'=>$gong['jobid'],
				'type'=>$gong['type'],
				'day'=>date('Y-m-d',time()),
				'address'=>$post['address'],
				'lat_lng'=>ijson_encode($post['lat_lng']),
				'starttime'=>TIMESTAMP,
				'uptime'=>TIMESTAMP,
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_aihire_signlog',$insert);
		$insert['id']=pdo_insertid();
		$this->upmaplog($insert,$post);
		return true;
	}
	public function endsign($gong,$post=array()){
		$day=date('Y-m-d',time());
		$item=pdo_get('jy_aihire_signlog',array('gongid'=>$gong['id'],'uniacid'=>$_SESSION['uniacid'],'endtime'=>0),'','createtime desc');
		if(!$item['id']){
			exi('不存在可打卡下班的记录！','error');
		}
		pdo_update('jy_aihire_signlog',array('endtime'=>TIMESTAMP),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$this->upmaplog($item,$post);
		return true;
	}
	public function upmaplog($signlog,$post=array()){
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'memberid'=>$signlog['memberid'],
				'mid'=>$signlog['mid'],
				'merchid'=>$signlog['merchid'],
				'gongid'=>$signlog['gongid'],
				'signid'=>$signlog['id'],
				'address'=>$post['address'],
				'lat_lng'=>ijson_encode($post['lat_lng']),
				'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_aihire_maplog',$insert);
		return true;
	}
}
?>